{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Union"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义学生信息\n",
    "names = ['张伟', '王娜', '王强', '赵敏', '刘洋', '王杰', '王帆', '吴昊', '周雪', '孙莉']\n",
    "genders = ['男', '女']\n",
    "hobbies_list = ['阅读', '游戏王', '篮球', '足球', '游泳', '跑步', '音乐']\n",
    "\n",
    "# 创建一个空的DataFrame来存储学生信息\n",
    "columns = ['姓名', '性别', '爱好']\n",
    "students_df = pd.DataFrame(columns=columns)\n",
    "\n",
    "def generate_random_students(df, names, genders, hobbies_list):\n",
    "    for name in names:\n",
    "        gender = random.choice(genders)\n",
    "        hobbies = random.sample(hobbies_list, 3)  # 每个学生随机选择3个爱好\n",
    "        hobbies_str = ', '.join(hobbies)\n",
    "        \n",
    "        new_student = pd.DataFrame([[name, gender, hobbies_str]], columns=columns)\n",
    "        df = pd.concat([df, new_student], ignore_index=True)\n",
    "    return df\n",
    "\n",
    "\n",
    "students_df = generate_random_students(students_df, names, genders, hobbies_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10, 3)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students_df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(students_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['张伟', '男', '足球, 音乐, 游泳'],\n",
       "       ['王娜', '男', '游戏王, 篮球, 游泳'],\n",
       "       ['王强', '女', '篮球, 游戏王, 游泳'],\n",
       "       ['赵敏', '男', '篮球, 游戏王, 阅读'],\n",
       "       ['刘洋', '女', '游戏王, 音乐, 篮球'],\n",
       "       ['王杰', '男', '游戏王, 跑步, 音乐'],\n",
       "       ['王帆', '男', '游戏王, 游泳, 跑步'],\n",
       "       ['吴昊', '女', '游泳, 足球, 音乐'],\n",
       "       ['周雪', '男', '游戏王, 音乐, 篮球'],\n",
       "       ['孙莉', '女', '阅读, 游泳, 足球']], dtype=object)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students_df.to_numpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>性别</th>\n",
       "      <th>爱好</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>张伟</td>\n",
       "      <td>男</td>\n",
       "      <td>足球, 音乐, 游泳</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>王娜</td>\n",
       "      <td>男</td>\n",
       "      <td>游戏王, 篮球, 游泳</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>王强</td>\n",
       "      <td>女</td>\n",
       "      <td>篮球, 游戏王, 游泳</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>赵敏</td>\n",
       "      <td>男</td>\n",
       "      <td>篮球, 游戏王, 阅读</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>刘洋</td>\n",
       "      <td>女</td>\n",
       "      <td>游戏王, 音乐, 篮球</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>王杰</td>\n",
       "      <td>男</td>\n",
       "      <td>游戏王, 跑步, 音乐</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>王帆</td>\n",
       "      <td>男</td>\n",
       "      <td>游戏王, 游泳, 跑步</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>吴昊</td>\n",
       "      <td>女</td>\n",
       "      <td>游泳, 足球, 音乐</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>周雪</td>\n",
       "      <td>男</td>\n",
       "      <td>游戏王, 音乐, 篮球</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>孙莉</td>\n",
       "      <td>女</td>\n",
       "      <td>阅读, 游泳, 足球</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   姓名 性别           爱好\n",
       "0  张伟  男   足球, 音乐, 游泳\n",
       "1  王娜  男  游戏王, 篮球, 游泳\n",
       "2  王强  女  篮球, 游戏王, 游泳\n",
       "3  赵敏  男  篮球, 游戏王, 阅读\n",
       "4  刘洋  女  游戏王, 音乐, 篮球\n",
       "5  王杰  男  游戏王, 跑步, 音乐\n",
       "6  王帆  男  游戏王, 游泳, 跑步\n",
       "7  吴昊  女   游泳, 足球, 音乐\n",
       "8  周雪  男  游戏王, 音乐, 篮球\n",
       "9  孙莉  女   阅读, 游泳, 足球"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def filter_by_keyword(df_: pd.DataFrame, cols: Union[list, str], keyword: str) -> pd.DataFrame:\n",
    "    if isinstance(cols, str):\n",
    "        cols = [cols]\n",
    "    idx = pd.Series([False] * len(df_))\n",
    "    for col in cols:\n",
    "        # df_ = df_[df_[col].str.contains(keyword, na=False)]\n",
    "        idx = idx | df_[col].str.contains(keyword, na=False)\n",
    "        print(idx)\n",
    "    return df_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1     True\n",
      "2     True\n",
      "3    False\n",
      "4    False\n",
      "5     True\n",
      "6     True\n",
      "7    False\n",
      "8    False\n",
      "9    False\n",
      "dtype: bool\n",
      "0    False\n",
      "1     True\n",
      "2     True\n",
      "3     True\n",
      "4     True\n",
      "5     True\n",
      "6     True\n",
      "7    False\n",
      "8     True\n",
      "9    False\n",
      "dtype: bool\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>性别</th>\n",
       "      <th>爱好</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>张伟</td>\n",
       "      <td>男</td>\n",
       "      <td>足球, 音乐, 游泳</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>王娜</td>\n",
       "      <td>男</td>\n",
       "      <td>游戏王, 篮球, 游泳</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>王强</td>\n",
       "      <td>女</td>\n",
       "      <td>篮球, 游戏王, 游泳</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>赵敏</td>\n",
       "      <td>男</td>\n",
       "      <td>篮球, 游戏王, 阅读</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>刘洋</td>\n",
       "      <td>女</td>\n",
       "      <td>游戏王, 音乐, 篮球</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>王杰</td>\n",
       "      <td>男</td>\n",
       "      <td>游戏王, 跑步, 音乐</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>王帆</td>\n",
       "      <td>男</td>\n",
       "      <td>游戏王, 游泳, 跑步</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>吴昊</td>\n",
       "      <td>女</td>\n",
       "      <td>游泳, 足球, 音乐</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>周雪</td>\n",
       "      <td>男</td>\n",
       "      <td>游戏王, 音乐, 篮球</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>孙莉</td>\n",
       "      <td>女</td>\n",
       "      <td>阅读, 游泳, 足球</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   姓名 性别           爱好\n",
       "0  张伟  男   足球, 音乐, 游泳\n",
       "1  王娜  男  游戏王, 篮球, 游泳\n",
       "2  王强  女  篮球, 游戏王, 游泳\n",
       "3  赵敏  男  篮球, 游戏王, 阅读\n",
       "4  刘洋  女  游戏王, 音乐, 篮球\n",
       "5  王杰  男  游戏王, 跑步, 音乐\n",
       "6  王帆  男  游戏王, 游泳, 跑步\n",
       "7  吴昊  女   游泳, 足球, 音乐\n",
       "8  周雪  男  游戏王, 音乐, 篮球\n",
       "9  孙莉  女   阅读, 游泳, 足球"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filter_by_keyword(students_df, [\"姓名\", \"爱好\"], \"王\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "companies_info = [\n",
    "    [\"氢能未来科技有限公司\", \"开发和制造氢燃料电池\"],\n",
    "    [\"氢能解决方案公司\", \"提供氢能储存和运输服务\"],\n",
    "    [\"绿色创新公司\", \"生产绿色氢气和氢气生成设备\"],\n",
    "    [\"氢能动力系统有限公司\", \"设计和销售氢能发电设备\"],\n",
    "    [\"纯氢实验室公司\", \"氢能研究和开发新材料\"],\n",
    "    [\"蓝氢能源公司\", \"提供氢能基础设施建设服务\"],\n",
    "    [\"氢动企业公司\", \"销售氢燃料电池车辆和相关配件\"],\n",
    "    [\"生态公司\", \"绿色能源生产及其工业应用\"],\n",
    "    [\"氢流技术公司\", \"能源管道和分配系统设计与维护\"],\n",
    "    [\"无限氢能公司\", \"开发综合氢能解决方案，包括发电和储能\"]\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(companies_info, columns=[\"企业名称\",\"经营范围\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>企业名称</th>\n",
       "      <th>经营范围</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>氢能未来科技有限公司</td>\n",
       "      <td>开发和制造氢燃料电池</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>氢能解决方案公司</td>\n",
       "      <td>提供氢能储存和运输服务</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>绿色创新公司</td>\n",
       "      <td>生产绿色氢气和氢气生成设备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>氢能动力系统有限公司</td>\n",
       "      <td>设计和销售氢能发电设备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>纯氢实验室公司</td>\n",
       "      <td>氢能研究和开发新材料</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         企业名称           经营范围\n",
       "0  氢能未来科技有限公司     开发和制造氢燃料电池\n",
       "1    氢能解决方案公司    提供氢能储存和运输服务\n",
       "2      绿色创新公司  生产绿色氢气和氢气生成设备\n",
       "3  氢能动力系统有限公司    设计和销售氢能发电设备\n",
       "4     纯氢实验室公司     氢能研究和开发新材料"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def filter_by_keyword1(df_: pd.DataFrame, cols: Union[list, str], keyword: str) -> pd.DataFrame:\n",
    "    if isinstance(cols, str):\n",
    "        cols = [cols]\n",
    "    idx = pd.Series([False] * len(df_))\n",
    "    for col in cols:\n",
    "        # df_ = df_[df_[col].str.contains(keyword, na=False)]\n",
    "        idx = idx | df_[col].str.contains(keyword, na=False)\n",
    "        # print(idx)\n",
    "    return df_[idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>企业名称</th>\n",
       "      <th>经营范围</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>氢能未来科技有限公司</td>\n",
       "      <td>开发和制造氢燃料电池</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>氢能解决方案公司</td>\n",
       "      <td>提供氢能储存和运输服务</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>氢能动力系统有限公司</td>\n",
       "      <td>设计和销售氢能发电设备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>纯氢实验室公司</td>\n",
       "      <td>氢能研究和开发新材料</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>蓝氢能源公司</td>\n",
       "      <td>提供氢能基础设施建设服务</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>氢动企业公司</td>\n",
       "      <td>销售氢燃料电池车辆和相关配件</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>氢流技术公司</td>\n",
       "      <td>能源管道和分配系统设计与维护</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>无限氢能公司</td>\n",
       "      <td>开发综合氢能解决方案，包括发电和储能</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         企业名称                经营范围\n",
       "0  氢能未来科技有限公司          开发和制造氢燃料电池\n",
       "1    氢能解决方案公司         提供氢能储存和运输服务\n",
       "3  氢能动力系统有限公司         设计和销售氢能发电设备\n",
       "4     纯氢实验室公司          氢能研究和开发新材料\n",
       "5      蓝氢能源公司        提供氢能基础设施建设服务\n",
       "6      氢动企业公司      销售氢燃料电池车辆和相关配件\n",
       "8      氢流技术公司      能源管道和分配系统设计与维护\n",
       "9      无限氢能公司  开发综合氢能解决方案，包括发电和储能"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filter_by_keyword1(df, \"企业名称\", \"氢\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>企业名称</th>\n",
       "      <th>经营范围</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>氢能未来科技有限公司</td>\n",
       "      <td>开发和制造氢燃料电池</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>氢能解决方案公司</td>\n",
       "      <td>提供氢能储存和运输服务</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>绿色创新公司</td>\n",
       "      <td>生产绿色氢气和氢气生成设备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>氢能动力系统有限公司</td>\n",
       "      <td>设计和销售氢能发电设备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>纯氢实验室公司</td>\n",
       "      <td>氢能研究和开发新材料</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>蓝氢能源公司</td>\n",
       "      <td>提供氢能基础设施建设服务</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>氢动企业公司</td>\n",
       "      <td>销售氢燃料电池车辆和相关配件</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>氢流技术公司</td>\n",
       "      <td>能源管道和分配系统设计与维护</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>无限氢能公司</td>\n",
       "      <td>开发综合氢能解决方案，包括发电和储能</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         企业名称                经营范围\n",
       "0  氢能未来科技有限公司          开发和制造氢燃料电池\n",
       "1    氢能解决方案公司         提供氢能储存和运输服务\n",
       "2      绿色创新公司       生产绿色氢气和氢气生成设备\n",
       "3  氢能动力系统有限公司         设计和销售氢能发电设备\n",
       "4     纯氢实验室公司          氢能研究和开发新材料\n",
       "5      蓝氢能源公司        提供氢能基础设施建设服务\n",
       "6      氢动企业公司      销售氢燃料电池车辆和相关配件\n",
       "8      氢流技术公司      能源管道和分配系统设计与维护\n",
       "9      无限氢能公司  开发综合氢能解决方案，包括发电和储能"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filter_by_keyword1(df, [\"企业名称\", \"经营范围\"], \"氢\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def filter_by_keyword2(\n",
    "    df_: pd.DataFrame, cols: Union[list, str], keyword: str) -> pd.DataFrame:\n",
    "    if isinstance(cols, str):\n",
    "        cols = [cols]\n",
    "    for col in cols:\n",
    "        df_ = df_[df_[col].str.contains(keyword, na=False)]\n",
    "    return df_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>企业名称</th>\n",
       "      <th>经营范围</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>氢能未来科技有限公司</td>\n",
       "      <td>开发和制造氢燃料电池</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>氢能解决方案公司</td>\n",
       "      <td>提供氢能储存和运输服务</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>氢能动力系统有限公司</td>\n",
       "      <td>设计和销售氢能发电设备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>纯氢实验室公司</td>\n",
       "      <td>氢能研究和开发新材料</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>蓝氢能源公司</td>\n",
       "      <td>提供氢能基础设施建设服务</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>氢动企业公司</td>\n",
       "      <td>销售氢燃料电池车辆和相关配件</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>无限氢能公司</td>\n",
       "      <td>开发综合氢能解决方案，包括发电和储能</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         企业名称                经营范围\n",
       "0  氢能未来科技有限公司          开发和制造氢燃料电池\n",
       "1    氢能解决方案公司         提供氢能储存和运输服务\n",
       "3  氢能动力系统有限公司         设计和销售氢能发电设备\n",
       "4     纯氢实验室公司          氢能研究和开发新材料\n",
       "5      蓝氢能源公司        提供氢能基础设施建设服务\n",
       "6      氢动企业公司      销售氢燃料电池车辆和相关配件\n",
       "9      无限氢能公司  开发综合氢能解决方案，包括发电和储能"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filter_by_keyword2(df, [\"企业名称\", \"经营范围\"], \"氢\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
